<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-04-01 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';
require_once ROOT_PATH.'include/lib/image.func.php';

class moduleObj extends masterObj
{
	private $hostel = '';
	
	function __construct($request)
	{
		parent::__construct($request);
		
		$hid = isset($this->request['hid']) ? (int)$this->request['hid'] : '';
		$info = get_hostel_info(array('hid'=>$hid));
		
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("酒店不存在", 'manage.php?m=hostel');
		}
		
		$this->hostel = $info;
		
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		$info = $this->hostel;
		$list = get_hostel_room_list(array('hid'=>$info['id']));
				
		include template('admin/hostel_room');
	}
	
	private function add()
	{
		$info = array();
		if(isset($this->request['c']))
		{
			$info = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$info['hostel_name'] = $this->hostel['name'];
		$info['name'] = isset($info['name']) ? $info['name'] : '';
		$info['price'] = isset($info['price']) ? $info['price'] : '';
		$info['area'] = isset($info['area']) ? $info['area'] : '';
		
		$result = get_room_types();
		$room_list = array();
		$room = array();
		foreach($result as $temp)
		{
			$room['name'] = $temp['name'];
			$room['value'] = $temp['id'];
			$room_list[] = $room;
		}
		$room_list = select('room_list', $room_list, '', 'onclick="select_room(\'room_list\')"');
		
		$action = 'do_add&hid='.$this->hostel['id'];
		$action_name = '添加房间';
		include template('admin/hostel_room_info');
	}
	
	private function doAdd()
	{
		//DBFunction::getInstance()->setDebug();
		
		$request['uid'] = $this->user['id'];
		$request['hid'] = $this->hostel['id'];
		$request['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		$request['price'] = isset($this->request['price']) ? (float)$this->request['price'] : 0;
		$request['area'] = isset($this->request['area']) ? (float)$this->request['area'] : 0;
		
		$request['image'] = '';
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "manage.php?m=hostel_room&a=add&hid=".$request['hid']."&c=$key";
		
		if(!$request['name'])
        {
			$this->messager("内容不能为空", $redirect);
		}
				
		if($_FILES['image']['name'])
		{
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
			if($image_path)
			{
				$request['image'] = $image_path;
			}
		}
		
		$result = add_hostel_room($request);
		
		if($result)
		{
			$this->messager("添加成功", 'manage.php?m=hostel_room&a=modify&hid='.$request['hid'].'&id='.$result);
		}
		else
		{
			$this->messager("添加失败", $redirect);
		}
	}
	
	private function doDelete()
	{
		//DBFunction::getInstance()->setDebug();
		
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		$redirect = "manage.php?m=hostel_room&hid=".$this->hostel['id'];
		
		if(!$ids)
		{
			$this->messager("请指定ID", $redirect);
		}
		
		$result = delete_hostel_room(array('hid'=>$this->hostel['id'], 'ids'=>$ids));

		if($result)
		{
			$this->messager("删除成功", $redirect);
		}
		else
		{
			$this->messager("删除失败", $redirect);
		}
	}
	
	private function modify()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=hostel_room&hid='.$this->hostel['id']);
		}
		
		if(isset($this->request['c']) && ($info = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$info = get_hostel_room(array('id'=>$id, 'hid'=>$this->hostel['id']));
			if(!isset($info['id']) || $info['id'] < 1)
			{
				$this->messager("房间不存在", 'manage.php?m=hostel_room&hid='.$this->hostel['id']);
			}
		}
		
		$result = get_room_types();
		$room_list = array();
		$room = array();
		foreach($result as $temp)
		{
			$room['name'] = $temp['name'];
			$room['value'] = $temp['id'];
			$room_list[] = $room;
		}
		$room_list = select('room_list', $room_list, '', 'onclick="select_room(\'room_list\')"');
		
		$info['hostel_name'] = $this->hostel['name'];
		
		$action = 'do_modify&hid='.$this->hostel['id'].'&id='.$id;
		$action_name = '编辑酒店房间';
		include template('admin/hostel_room_info');
	}
	
	private function doModify()
	{
		//DBFunction::getInstance()->setDebug();
		
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=hostel_room&hid='.$this->hostel['id']);
		}
		
		$info = get_hostel_room(array('id'=>$id, 'hid'=>$this->hostel['id']));
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("房间不存在", 'manage.php?m=hostel_room&hid='.$this->hostel['id']);
		}
		
		$request['uid'] = $this->user['id'];
		$request['hid'] = $this->hostel['id'];
		$request['id'] = $id;
		
		$request['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		$request['price'] = isset($this->request['price']) ? (float)$this->request['price'] : 0;
		$request['area'] = isset($this->request['area']) ? (float)$this->request['area'] : 0;
		$request['image'] = '';
				
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "manage.php?m=hostel_room&a=add&hid=".$request['hid']."&c=$key";
		
		if(!$request['name'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		
		$request['uid'] = $this->user['id'];

		if($_FILES['image']['name'])
		{
			$old_image = $info['image'];
			
			if(is_file($old_image))
			{
				delete_image($old_image);
			}
			
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
			if($image_path)
			{
				$request['image'] = $image_path;
			}
		}
		else
		{
			$request['image'] = $info['image'];
		}
		
		$result = update_hostel_room($request);
		
		if($result)
		{
			$this->messager("修改成功", "manage.php?m=hostel_room&hid=".$request['hid']);
		}
		else
		{
			$this->messager("修改失败", "manage.php?m=hostel_room&hid=".$request['hid']);
		}
	}
}